﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Models;

namespace ChinaDynastyTours.Views
{
    public partial class GuideEvaluateForm : Form
    {
        private DataTable _dtGuideEvaluateData;
        private string _TourgroupID { get; set; }

        public GuideEvaluateForm(string tourgroupId)
        {
            InitializeComponent();
            _TourgroupID = tourgroupId;
        }
 
        private void GuideEvaluateForm_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_TourgroupID))//如果没传进来团号
            {
                //初始化团下拉列表
                DataSet ds = TeamGroupCtrl.GetAllIDForTeamGroup();
                if (ds.Tables.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                        btnTeamGroupID.Properties.Items.Add(dr["TourGroupID"].ToString().Trim());
                }
            }
            else
            {
                btnTeamGroupID.Text = _TourgroupID; 
                btnTeamGroupID.Enabled = false;
                guideGridView.OptionsBehavior.Editable = false;
                guideGridControl.DataSource = GuideEvaluateCtrl.GetGuideEvaluateData(_TourgroupID);
            }
        }

        private void btnTeamGroupID_SelectedIndexChanged(object sender, EventArgs e)
        {
            _dtGuideEvaluateData = GuideEvaluateCtrl.GetGuideEvaluateData(btnTeamGroupID.Text);

            if (_dtGuideEvaluateData == null || _dtGuideEvaluateData.Rows.Count == 0)
            {
                string tourgroupName = TeamGroupCtrl.GetTeamGroupNameById(btnTeamGroupID.Text);

                //获取行程计划中旅行社、宾馆、目的地信息
                DataTable tmpDt = GuideEvaluateCtrl.GetTourGroupScheduleForGuideEval(btnTeamGroupID.Text).Tables[0];

                foreach (DataRow dr in tmpDt.Rows)
                {
                    GuideEvaluateEntity entity = new GuideEvaluateEntity()
                    {
                        TourgroupID = btnTeamGroupID.Text,
                        TourgroupName = tourgroupName,
                        CiceroniID = int.Parse(dr["CiceroniID"].ToString()),
                        CiceroniName = dr["CiceroniName"].ToString(),
                        TravelAgencyName = dr["TravelAgencyName"].ToString(),
                        Good = 0,
                        Middle = 0,
                        Common = 0,
                        Bad = 0,
                        Praise = 0,
                        Complain = 0,
                        Remark = "",
                    };
                    GuideEvaluateCtrl.CreateGuideEvaluateData(entity);
                }
                _dtGuideEvaluateData = GuideEvaluateCtrl.GetGuideEvaluateData(btnTeamGroupID.Text);
            }
            //加载数据到UI的gride
            guideGridControl.DataSource = _dtGuideEvaluateData;
            _dtGuideEvaluateData.RowChanged += new DataRowChangeEventHandler(_dtHotelEvaluateData_RowChanged);
        }

        void _dtHotelEvaluateData_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Change || e.Action == DataRowAction.ChangeCurrentAndOriginal || e.Action == DataRowAction.ChangeOriginal)
            {
                //string ProviderID = feedbackGridView.GetRowCellValue(feedbackGridView.FocusedRowHandle, "ProviderID").ToString();

                GuideEvaluateEntity entity = new GuideEvaluateEntity()
                {
                    EvaluateID = int.Parse(guideGridView.GetRowCellValue(guideGridView.FocusedRowHandle, "EvaluateID").ToString()),
                    TourgroupID = e.Row["TourgroupID"].ToString(),
                    TourgroupName = e.Row["TourgroupName"].ToString(),
                    TravelAgencyName = e.Row["TravelAgencyName"].ToString(),
                    CiceroniID = int.Parse(e.Row["CiceroniID"].ToString()),
                    CiceroniName = e.Row["CiceroniName"].ToString(),
                    Good = int.Parse(e.Row["Good"].ToString()),
                    Middle = int.Parse(e.Row["Middle"].ToString()),
                    Common = int.Parse(e.Row["Common"].ToString()),
                    Bad = int.Parse(e.Row["Bad"].ToString()),
                    Praise = int.Parse(e.Row["Praise"].ToString()),
                    Complain = int.Parse(e.Row["Complain"].ToString()),
                    Remark = e.Row["Remark"].ToString()
                };
                GuideEvaluateCtrl.UpdateGuideEvaluateData(entity);
            }
        }
    }
}
